package com.xiaoy.core.basic.security.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaoy.core.basic.security.entity.Role;
import com.xiaoy.dao.DaoUtil;

@Service("roleService")
public class RoleService {

	private String tablename = "BAS_SECURITY_ROLE t ";
	@Resource(name="daoUtil")
	private DaoUtil<Role, Long>daoUtil;


	public List<Role> getRoleTree () {
		
		String sql = "select * from "+tablename+" where t.pid is null";
		List<Role>roles = daoUtil.query(Role.class,sql);
		return roles;
	}
	
	public Role getById (long id) {
		return (Role)daoUtil.getSession().get(Role.class,id);
	}
	
	@Transactional
	public void save (Role role,long pid) {
		Role p = getById(pid);
		role.setParent(p);
		daoUtil.save(role);
	}
	
	public Role delete (long id) {
		
		Role role = getById(id);
		daoUtil.delete(role);
		return role;
	}
	
	
}
